﻿using System;
using NUnit.Framework;
using Pyxis.FluentSelenium.Tasks;
using Selenium;

namespace Pyxis.FluentSelenium.SampleWebAppTest.Forum
{
    [TestFixture]
    public class AddPostsTest
    {
        private WebApplication forumApplication;
        private User forumUser;

        private static readonly ITask AddNewPost = new AddNewPostTask("Luc Dorval", "Simple example of the library.", "");

        [SetUp]
        public void Initialise()
        {
            forumApplication = new WebApplication(3000, "Pyxis.FluentSelenium.SampleWebApp");
            forumApplication.Start();

            var selenium = new DefaultSelenium("127.0.0.1", 4445, "*iexplore", "http://localhost:3000/");
            forumUser = new User(selenium);
            forumUser.Start();
        }

        [TearDown]
        public void Cleanup()
        {
            forumUser.Stop();
            forumApplication.Stop();
        }

        [Test]
        public void TestAddNewPostCanSeeInTheList()
        {
            forumUser.Goto("Forum/AllPosts.aspx");
            forumUser.For(PostListPage.AddPostButton).Clicks();

            forumUser.For(NewPostPage.AuthorTextBox).Enters("Luc Dorval");
            forumUser.For(NewPostPage.SubjectTextBox).Enters("Simple example of the library.");
            forumUser.For(NewPostPage.MessagetTextBox).Enters("This code should add a new post to the list.");

            var postDate = DateTime.Now.ToString("yyyy.MM.dd");
            forumUser.For(NewPostPage.SavePostButton).Clicks();
            forumUser.For(PostListPage.AllPostsTable).Row(2).ShouldSee(postDate, "Luc Dorval", "Simple example of the library.");
        }

        [Test]
        public void TestAddNewPostWithoutAnAuthorShowsErrorMessage()
        {
            forumUser.Goto("Forum/AllPosts.aspx");
            forumUser.For(PostListPage.AddPostButton).Clicks();

            forumUser.For(NewPostPage.SubjectTextBox).Enters("Simple example of the library.");
            forumUser.For(NewPostPage.MessagetTextBox).Enters("This code should add a new post to the list.");
            forumUser.For(NewPostPage.SavePostButtonWithoutWait).Clicks();
            forumUser.For(NewPostPage.AuthorRequiredMessage).ShouldSee("The author is required.");
        }

        [Test]
        public void TestSortByAuthorShowTheListSortedByAuthorAscending()
        {
            forumUser.Goto("Forum/AllPosts.aspx");
            forumUser.For(PostListPage.AddPostButton).Clicks();

            forumUser.For(NewPostPage.AuthorTextBox).Enters("Luc Dorval");
            forumUser.For(NewPostPage.SubjectTextBox).Enters("A sorting test.");

            forumUser.For(NewPostPage.SavePostButton).Clicks();

            forumUser.For(PostListPage.AddPostButton).Clicks();

            forumUser.For(NewPostPage.AuthorTextBox).Enters("Erik LeBel");
            forumUser.For(NewPostPage.SubjectTextBox).Enters("Simple sorting test.");

            forumUser.For(NewPostPage.SavePostButton).Clicks();

            forumUser.For(PostListPage.AllPostsTable).ShouldCountRows(3);
            forumUser.For(PostListPage.AllPostsTable).ColumnHeader(2).For(PostListPage.SortAuthorButton).Clicks();
            forumUser.For(PostListPage.AllPostsTable).Column(2).ShouldSee("Erik LeBel", "Luc Dorval");
        }

        [Test]
        public void TestAddNewPostUsingATaskCanSeeInTheList()
        {
            forumUser.Goto("Forum/AllPosts.aspx");
            forumUser.Do(AddNewPost);

            var postDate = DateTime.Now.ToString("yyyy.MM.dd");
            forumUser.For(PostListPage.AllPostsTable).Row(2).ShouldSee(postDate, "Luc Dorval", "Simple example of the library.");
        }
    }
}